import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:io';

// 自定义store类
import './model/my_store.dart';

// 页面 pages
import './pages/home_page.dart';
import './pages/login.dart';
void main() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  MyStore.instance = MyStore(prefs1: prefs);
  MyStore().setData('TEST', {
    'msg' : '测试数据'
  });
  runApp(MyApp());
  
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  } 
}

// class MyApp extends StatelessWidget {
//   @override

// }

class MyApp extends StatefulWidget {
  _MyAppState createState() => _MyAppState();
}

class SetTheme {
  SetTheme({
    this.setColor,
  });
  Function setColor;
}

class _MyAppState extends State<MyApp> {
  ThemeData themeColor;
  SetTheme _setTheme;
  @override
  void initState(){
    super.initState();
    _setTheme = SetTheme(
      setColor: setThemeColor
    );
    MyStore().setGlobal('_setTheme', _setTheme);
    if (MyStore().getData('THEME') != null) {
      Color _colors = MyStore().themeList[MyStore().getData('THEME')['index']];
      themeColor = ThemeData( // 主题设
        primarySwatch: _colors
      );
    } else {
      MyStore().setData('THEME', {'index': 2});
      themeColor = ThemeData( // 主题色
        primarySwatch: Colors.blue
      );
    }
  }
  setThemeColor (Color themtColor) {
    setState(() {
      themeColor = ThemeData( // 主题色
        primarySwatch:themtColor
      );     
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '海草',
      debugShowCheckedModeBanner: false,
      theme: themeColor,
      home: MyHome(
        setTheme: _setTheme
      )
    );
  }
}

class MyHome extends StatefulWidget {
  MyHome({
    Key key,
    this.setTheme
  }):super(key: key);
  dynamic setTheme;
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1500)..init(context);
    if (MyStore().getData('LOGININFO') == null) {
      return Login(
        setTheme:widget.setTheme
      );
    }
    return HomePage(
      setTheme:widget.setTheme
    );
  }
}